package org.springframework.example.t0830.scope;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 模拟作用域
 */
public class MyScopeHandler implements Scope {

	private Map<String, Object> cache = new ConcurrentHashMap<>();

	@Override
	public Object get(String name, ObjectFactory<?> objectFactory) {
		Object o = cache.get(name);
		if (o == null) {
			Object object = objectFactory.getObject();
			if (object != null) {
				cache.put(name, object);
			}
			return object;

		} else {
			return o;
		}
	}

	@Override
	public Object remove(String name) {
		return cache.remove(name);
	}

	@Override
	public void registerDestructionCallback(String name, Runnable callback) {

	}

	@Override
	public Object resolveContextualObject(String key) {
		return null;
	}

	@Override
	public String getConversationId() {
		return null;
	}
}
